﻿/// <reference path="jquery/jquery-1.4.1.js"/>
var avlmsg = "";
InitializeAvl();

$(function () {
    InitializeAvl();
    prm.add_endRequest(function (s, a) {
        CallOM(avlmsg);
    });

    $('.submitcommentbutton').click(function () {
        SubmitComment(
        $(this).find('#appName').val(),
        $(this).find('#contextName').val(),
        $(this).find('#contextId').val(),
        $(this).find('#containerId').val()
        );
    });

    $('.commentcontrol').each(function () {
        ReloadComment(
                    $(this).find('#appName').val(),
                    $(this).find('#contextName').val(),
                    $(this).find('#contextId').val(),
                    $(this).find('#containerId').val()
                    );
    });

    BindDeleteButton();
});

function BindDeleteButton() {
    $('.delcomment').click(function () {
        var a = $(this).parents('.c-thread');
        var tid = $(this).attr('tid');
        $.post('/Services/Services.asmx/DeleteCommentThread', { threadId: tid }, function (data, s, x) {
            if (s == 'success') {
                a.remove();
            }
        });
    });
}

function InitializeAvl() {
    $('a.savecomment').click(function() {
        avlmsg = "已发表一条评论。";
    });

    $('a.delcomment').click(function() {
        avlmsg = "已删除一条评论。";
    });
}

function SubmitComment(appName, contextName, contextId, containerId) {
    var editor = GetTinyMceEditor($('.commenteditor').attr("id"));
    
    $.ajax({
        type: 'POST',
        url: relRootPath + "/Services/Services.asmx/AddCommentThread",
        async: false,
        success: CommentSuccess,
        data: {
            appName: appName,
            contextName: contextName,
            contextId: contextId,
            userId: userid,
            content: editor.getContent()
        },
        context:
        {
            appName: appName,
            contextName: contextName,
            contextId: contextId,
            container : $('div#' + containerId)
        }
    });

    editor.setContent('');
}

function CommentSuccess(data, status, x)
{
    ReloadComment(this.appName, this.contextName, this.contextId, this.container);
}

function ReloadComment(appName, contextName, contextId, container) {
    //GetTinyMceEditor($('.commenteditor').attr("id")).setContent('');
    $.ajax({
        type: 'POST',
        url: relRootPath + "/Services/Services.asmx/GetThreadsByContext",
        data: {
            appName: appName,
            contextName: contextName,
            contextId: contextId
        },
        success: function (data, s, x) {
            RenderComments(container, eval($(data).text()));
            Utility.DisplayAvatar();
        }
    });
}

function RenderComments(containerId, response) {
    
    var container = (containerId instanceof jQuery) ? containerId : $('#' + containerId);
    container.empty();
    $.each(response, function (key, value) {
        var ct = "<div class='c-thread fc'><div class='c-wrapper'>";
        var tid;
        var canDelete = false;
        var cnt = 0;
        $.each(value, function (k, v) {
            canDelete = cnt++ == 0 && v.ComposerUserId == userid;
            var ms = v.CommentDate.slice(6, 19);
            var cdate = new Date(parseInt(ms));
            tid = v.ThreadId;
            ct += "<div class='c-entry'><div class='left'><div class='c-avatar avtb' type='tiny'><span title='" + v.ComposerUserId + "' class='uid'></span></div></div>";
            ct += "<div class='c-entrybody'><div class='left'></div><div class='datetext right ftime'>" + cdate + "</div>";
            ct += "<div>" + v.CommentContent + "</div></div>";
            ct += "</div>";
        });

        if (canDelete) {
            ct += "<div class='clear ctn-p-t textfar'><a class='delcomment pointer' tid='" + tid + "'>删除</a></div>";
        }
        ct += "</div></div>";
        $(ct).appendTo(container);

    });

    Utility.RenderAvatar();
    RenderNickName();
    $('.ftime').cuteTime();
    BindDeleteButton();
}

function RenderNickName() {
    $('.c-entry').each(function () {
        var nickname = $(this).find('.thumbimgbox img').attr('title');
        $(this).find('.thumbimgbox a').clone().empty().appendTo($(this).find('span.composer'));
        $(this).find('span.composer a').text(nickname);
    });
}

